package weaver.interfaces.jjy.duss.action;

import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.interfaces.jjy.duss.hr.ZFM_HR_BZ_ZB;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2020-05-10
 * @Description: 编制增补推送SAP
 * @ModifyLog:
 **/

public class WeaHRBzzbtsAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        RecordSet rs1 = new RecordSet();
        BaseBean baseBean = new BaseBean();
        List<Map<String, String>> list = new ArrayList<>();
        String requestid = requestInfo.getRequestid();
        baseBean.writeLog("请求ID" + requestid);
        baseBean.writeLog("调用WeaHRBzzbtsAction");
        String tablename = requestInfo.getRequestManager().getBillTableName();//主表
        rs.executeQuery("select id,zehr_ztwb,zehr_fsrq from " + tablename + " where requestid=?", requestid);
        baseBean.writeLog("select id,zehr_ztwb,zehr_fsrq from " + tablename + " where requestid=" + requestid);
        rs.next();
        //主表字段
        String mainid = Util.null2String(rs.getString("id"));//外键
        String ZEHR_ZTWB = Util.null2String(rs.getString("ZEHR_ZTWB"));//状态
        String ZEHR_FSRQ = Util.null2String(rs.getString("ZEHR_FSRQ").replaceAll("-",""));//发送日期
        String BZLX = "2";
        //明细表1
        rs1.executeQuery("select zehr_numid_bz,zehr_hdbzs_g,zehr_hdbzs_z,zehr_hdbzs_m,zehr_hdbzs_j,zehr_sbrs_g,zehr_sbrs_z,zehr_sbrs_m,zehr_sbbzs_j from " + tablename + "_dt1 where mainid='"+mainid+"'");
        baseBean.writeLog("select zehr_numid_bz,zehr_hdbzs_g,zehr_hdbzs_z,zehr_hdbzs_m,zehr_hdbzs_j,zehr_sbrs_g,zehr_sbrs_z,zehr_sbrs_m,zehr_sbbzs_j from " + tablename + "_dt1 where mainid='"+mainid+"'");
        while (rs1.next()) {
            String zehr_numid_bz = Util.null2String(rs1.getString("zehr_numid_bz"));//唯一标识
            String zehr_hdbzs_g = Util.null2String(rs1.getString("zehr_hdbzs_g"));//核定人数（高层）
            String zehr_hdbzs_z = Util.null2String(rs1.getString("zehr_hdbzs_z"));//核定编制数（中层）
            String zehr_hdbzs_m = Util.null2String(rs1.getString("zehr_hdbzs_m"));//核定编制数（主管）
            String zehr_hdbzs_j = Util.null2String(rs1.getString("zehr_hdbzs_j"));//核定编制数（基层）
            String zehr_sbrs_g = Util.null2String(rs1.getString("zehr_sbrs_g"));//申报人数（高层）
            String zehr_sbrs_z = Util.null2String(rs1.getString("zehr_sbrs_z"));//申报人数（中层）
            String zehr_sbrs_m = Util.null2String(rs1.getString("zehr_sbrs_m"));//申报人数（主管）
            String zehr_sbbzs_j = Util.null2String(rs1.getString("zehr_sbbzs_j"));//申报人数（基层）
            Map<String, String> map = new HashMap<>();
            map.put("NUMID" , zehr_numid_bz);    //唯一标识
            map.put("SXRQ" , ZEHR_FSRQ);         //生效日期
            map.put("BZLX" , BZLX);              //编制类型
            map.put("ZT" , ZEHR_ZTWB);           //状态文本
            map.put("HDRS_G" , zehr_hdbzs_g);    //核定编制数（高层）
            map.put("HDRS_Z" , zehr_hdbzs_z);    //核定编制数（中层）
            map.put("HDRS_M" , zehr_hdbzs_m);    //核定编制数（主管）
            map.put("HDRS_J" , zehr_hdbzs_j);    //核定编制数（基层）
            map.put("SBRS_G" , zehr_sbrs_g);     //申报人数（高层）
            map.put("SBRS_Z" , zehr_sbrs_z);     //申报人数（中层）
            map.put("SBRS_M" , zehr_sbrs_m);     //申报人数（主管）
            map.put("SBRS_J" , zehr_sbbzs_j);    //申报编制数（基层）
            baseBean.writeLog("参数map=" + map.toString());
            list.add(map);
        }
        baseBean.writeLog("参数list=" + list.toString());
        try {
            ZFM_HR_BZ_ZB bz_zb = new ZFM_HR_BZ_ZB();
            Map<String, String> resultMap = bz_zb.execute(list);
            String flag = Util.null2String(resultMap.get("flag"));
            String message = Util.null2String(resultMap.get("message"));
            baseBean.writeLog("SAP返回结果——flag:" + flag + ";message:" + message);
            if ("E".equalsIgnoreCase(flag)) {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("SAP返回信息提示:"+message);
                return Action.FAILURE_AND_CONTINUE;
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            baseBean.writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }

    }
}
